package com.hmeter.mongodb.gui;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.mongodb.MongoDbDeleteSampler;

public class MongoDbDeleteSamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = 522709729259732877L;
	
	private JLabeledTextField hostTextField = null;
	private JLabeledTextField portTextField = null;
	private JLabeledTextField dbNameTextField = null;
	private JLabeledTextField collectionNameTextField = null;
	private JLabeledTextField queryListTextField = null;
	private JLabeledTextField operatorTextField = null;
	private JLabeledTextField delimiterTextField = null;
	private JCheckBox deleteAllDocumentsFromCollectionCheckBox = null;
	private JCheckBox deleteDocumentsFromCollectionByClauseCheckBox = null;
	
	public MongoDbDeleteSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
        
        hostTextField = new JLabeledTextField("Host");
        portTextField = new JLabeledTextField("Port");
        dbNameTextField = new JLabeledTextField("MongoDB name");
        collectionNameTextField = new JLabeledTextField("Collection name");
        queryListTextField = new JLabeledTextField("List of queries (format: key|operator|value, $eq if operator=equals)");
        operatorTextField = new JLabeledTextField("Sum operator");
        delimiterTextField = new JLabeledTextField("Delimiter");
        deleteAllDocumentsFromCollectionCheckBox = new JCheckBox("Delete all documents from collection");
        deleteDocumentsFromCollectionByClauseCheckBox = new JCheckBox("Delete documents from collection by clause");
        ButtonGroup options = new ButtonGroup();
        options.add(deleteAllDocumentsFromCollectionCheckBox);
        options.add(deleteDocumentsFromCollectionByClauseCheckBox);

        //register events
        changeTextFieldState(deleteDocumentsFromCollectionByClauseCheckBox, queryListTextField);
        changeTextFieldState(deleteDocumentsFromCollectionByClauseCheckBox, delimiterTextField);
        changeTextFieldState(deleteDocumentsFromCollectionByClauseCheckBox, operatorTextField);
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        inputPanel.add(hostTextField);
        inputPanel.add(portTextField);
        inputPanel.add(dbNameTextField);
        inputPanel.add(collectionNameTextField);
        
        //delete all documents panel
        VerticalPanel deleteAllDocumentsPanel = new VerticalPanel();
        deleteAllDocumentsPanel.setBorder(BorderFactory.createTitledBorder("Delete all documents"));
        deleteAllDocumentsPanel.add(deleteAllDocumentsFromCollectionCheckBox);

        //delete documents by clause panel
        VerticalPanel deleteDocumentByClausePanel = new VerticalPanel();
        deleteDocumentByClausePanel.setBorder(BorderFactory.createTitledBorder("Delete documents by clause"));
        deleteDocumentByClausePanel.add(deleteDocumentsFromCollectionByClauseCheckBox);
        deleteDocumentByClausePanel.add(queryListTextField);
        deleteDocumentByClausePanel.add(delimiterTextField);
        deleteDocumentByClausePanel.add(operatorTextField);
        
        panel.add(inputPanel);
        panel.add(deleteAllDocumentsPanel);
        panel.add(deleteDocumentByClausePanel);
        
        add(panel, BorderLayout.CENTER);
	}
	
	public void changeTextFieldState(JCheckBox checkBox, final JLabeledTextField textField)
	{
		checkBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					textField.setEnabled(true);
				}
				else if (e.getStateChange() == ItemEvent.DESELECTED)
				{
					textField.setEnabled(false);
				}
			}
		}
		);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		hostTextField.setText("");
		portTextField.setText("");
		dbNameTextField.setText("");
		collectionNameTextField.setText("");
		queryListTextField.setText("");
		operatorTextField.setText("");
		delimiterTextField.setText("");
		deleteAllDocumentsFromCollectionCheckBox.setSelected(true);
		deleteDocumentsFromCollectionByClauseCheckBox.setSelected(false);
		queryListTextField.setEnabled(false);
		operatorTextField.setEnabled(false);
		delimiterTextField.setEnabled(false);
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "MongoDB Delete Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		MongoDbDeleteSampler mongoDbDeleteSampler = new MongoDbDeleteSampler();
		modifyTestElement((TestElement) mongoDbDeleteSampler);
		return (TestElement) mongoDbDeleteSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof MongoDbDeleteSampler)
		{
			MongoDbDeleteSampler mongoDbDeleteSampler = (MongoDbDeleteSampler) element;
			mongoDbDeleteSampler.setHost(hostTextField.getText());
			mongoDbDeleteSampler.setPort(portTextField.getText());
			mongoDbDeleteSampler.setDbName(dbNameTextField.getText());
			mongoDbDeleteSampler.setCollectionName(collectionNameTextField.getText());
			mongoDbDeleteSampler.setQueryList(queryListTextField.getText());
			mongoDbDeleteSampler.setOperator(operatorTextField.getText());
			mongoDbDeleteSampler.setDelimiter(delimiterTextField.getText());
			mongoDbDeleteSampler.setDeleteAllDocumentsFromCollectionBool(deleteAllDocumentsFromCollectionCheckBox.isSelected());
			mongoDbDeleteSampler.setDeleteDocumentsFromCollectionByClauseBool(deleteDocumentsFromCollectionByClauseCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof MongoDbDeleteSampler)
		{
			MongoDbDeleteSampler mongoDbDeleteSampler = (MongoDbDeleteSampler) element;
			hostTextField.setText(mongoDbDeleteSampler.getHost());
			portTextField.setText(mongoDbDeleteSampler.getPort());
			dbNameTextField.setText(mongoDbDeleteSampler.getDbName());
			collectionNameTextField.setText(mongoDbDeleteSampler.getCollectionName());
			queryListTextField.setText(mongoDbDeleteSampler.getQueryList());
			operatorTextField.setText(mongoDbDeleteSampler.getOperator());
			delimiterTextField.setText(mongoDbDeleteSampler.getDelimiter());
			deleteAllDocumentsFromCollectionCheckBox.setSelected(mongoDbDeleteSampler.isDeleteAllDocumentsFromCollectionBool());
			deleteDocumentsFromCollectionByClauseCheckBox.setSelected(mongoDbDeleteSampler.isDeleteDocumentsFromCollectionByClauseBool());
		}
	}
}
